#ifndef  SIM900A_CORE_H
#define  SIM900A_CORE_H

#include <stdarg.h>
#include <stdint.h>
#include "dev_platform.h"

#define     LAST_CMD_MAX_LEN  100
#define     DATA_BUFFER_MAX_LEN  500
#define     SERIAL_INPUT_BUFFER_MAX_LEN  500 //串口输入缓冲区

#define     SIM900A_DEFAULT_CMD_TIMEOUT 3000    //默认的命令行执行超时时间

#define     SIM900A_run(cmdline, timeout, ret_content, proc)  \
    SIM900A_perform_formatted (timeout, ret_content, proc, "%s", cmdline) //执行

typedef struct cmdline
{
    char busy;                                    //同步标识
    char dest[DATA_BUFFER_MAX_LEN];               //命令执行过程中产生的中间数据
    int  cmdline_running;                         //发送的指令运行状态
    int  retstat;                                  //命令行返回状态值
    char *buffer;                                 //临时输入缓冲区
    void (*cmd_callback)(int retstat);            //输入过的命令行执行结束后回调
    void (*idle_callback)(char *buffer);          //空闲情况下收到的信息处理
}SIM900A_cmdline_t;

//串口接收数据结构
typedef struct serial_buffer
{
    int index;              //缓冲区索引
    char buffer[SERIAL_INPUT_BUFFER_MAX_LEN];   //输入缓冲区
    char last;              //上一个字符
    char recv_en;              //接收使能
}serial_buffer_t;


#if (PLATFORM == LINUX)
void SIM900A_core_uprintf(char *fmt, ...);
void SIM900A_core_uvprintf(char *fmt, va_list va);
#define     DBG_PRINTF(...)   {\
    fprintf(stdout, "[\033[01;31mDEBUG\033[0m]\033[01;36m"); \
    fprintf(stdout,__VA_ARGS__); \
    fprintf(stdout, "\033[00m"); \
    fflush (stdout);\
}
#define     DBG_VPRINTF(...)   {\
    fprintf(stdout, "\033[01;31m[DEBUG]\033[01;36m"); \
    vfprintf(stdout,__VA_ARGS__); \
    fprintf(stdout, "\033[00m"); \
    fflush (stdout);\
}
//#define     uprintf(...)     SIM900A_core_printf(__VA_ARGS__)
//#define     uvprintf(...)    SIM900A_core_vprintf(__VA_ARGS__)

#elif (PLATFORM == LPC1114) 
#define     DBG_PRINTF(...)   ((void)(0))
#define     DBG_VPRINTF(...)   ((void)(0))
#define     SIM900A_core_uprintf(...)    printf(__VA_ARGS__)
#define     SIM900A_core_uvprintf(...)    vprintf(__VA_ARGS__)
#endif


//SIM900A返回错误
typedef enum  
{  
    AT_RETURN_TIME_OUT      =    -2,      //等待返回超时  
    AT_RETURN_ERROR         =    -1,      //返回错误  
    AT_RETURN_UNKNOWN       =     0,      //返回结果未知  
    AT_RETURN_OK            =     1,      //返回成功  
    AT_RETURN_CON           =     2,      //等待下一阶段输入
    AT_RETURN_BUSY          =     3,      //指令执行正忙
    AT_RETURN_SENDOK        =     4,      //gprs的sendok
    AT_RETURN_CONNECTED     =     5,      //gprs的sendok
    AT_RETURN_SHUTOK        =     6,      //gprs的sendok
    AT_RETURN_CLOSEOK       =     7,      //gprs的closeok
}SIM900_ERROR; 

//AT指令接收到命令的处理
typedef struct _at_recv_cb
{
    char *keyword;
    int (*callback)(char *buffer);
}at_recv_cb_t;


void  SIM900A_core_disable_uart_recv(void);
void  SIM900A_core_enable_uart_recv(void);
//空闲情况下收到的信息处理
void  SIM900A_core_set_passive_cb(void (*idle_callback)(char *buffer));
void  SIM900A_core_init(void);//sim900a内核初始化
char* SIM900A_core_get_cmdline_dest(void);   //获取命令行执行过程中的数据 
void  SIM900A_core_clean_uart_rxbuf(void);  
void  SIM900A_core_getch(uint8_t byte);       //处理串口读来的字节数据
int   SIM900A_perform_formatted(int timeout, char **ret_content, int (*proc)(char *, int), 
        const char *fmt, ...);

#endif  /*SIM900A_CORE_H*/
